#!/bin/sh
#
# Set needed variables
##
BIN_DIR="bin"
BINARY_NAME="Qedit"
BUNDLE_APP_PATH=$BIN_DIR/$BINARY_NAME.app
BUNDLE_RESOURCES_PATH=$BUNDLE_APP_PATH/Contents/Resources
BUNDLE_FRAMEWORK_PATH=$BUNDLE_APP_PATH/Contents/Frameworks
BUNDLE_EXE=$BUNDLE_APP_PATH/Contents/MacOS/$BINARY_NAME
BUNDLE_FRAMEWORK_PREFIX=@executable_path/../Frameworks
OSVERSION=$(uname -r)


##
# $1: full path to the lib
##
changeBUNDLEInstallName()
{
	if [ $# -eq 1 ]; then
		install_name_tool -change $1 $BUNDLE_FRAMEWORK_PREFIX/`basename $1` $BUNDLE_EXE
	fi

	if [ $# -eq 2 ]; then
		install_name_tool -change $1 $BUNDLE_FRAMEWORK_PREFIX/$2 $BUNDLE_EXE
	fi
}

changeInstallName()
{
	install_name_tool -change $1 $BUNDLE_FRAMEWORK_PREFIX/`basename $1` $2
}

testAndMkdir()
{

	if [ ! -d $1 ]; then
		mkdir -p $1
	fi
}

##
# Copy the bundle into the desired one
##

testAndMkdir $BUNDLE_APP_PATH




##
##
# Creates the needed directories if they do not exist
##
testAndMkdir $BUNDLE_FRAMEWORK_PATH
testAndMkdir $BUNDLE_FRAMEWORK_PATH/QtCore.framework/Versions/4.0
testAndMkdir $BUNDLE_FRAMEWORK_PATH/QtGui.framework/Versions/4.0
testAndMkdir $BUNDLE_FRAMEWORK_PATH/QtXml.framework/Versions/4.0
testAndMkdir $BUNDLE_FRAMEWORK_PATH/QtSvg.framework/Versions/4.0
testAndMkdir $BUNDLE_FRAMEWORK_PATH/qt-plugins/imageformats

testAndMkdir $BUNDLE_FRAMEWORK_PATH/phapi-plugins

testAndMkdir $BUNDLE_RESOURCES_PATH

##
# Copy needed frameworks
##
cp $QTDIR/lib/QtCore.framework/Versions/4.0/QtCore $BUNDLE_FRAMEWORK_PATH/QtCore.framework/Versions/4.0/QtCore
cp $QTDIR/lib/QtGui.framework/Versions/4.0/QtGui $BUNDLE_FRAMEWORK_PATH/QtGui.framework/Versions/4.0/QtGui
cp $QTDIR/lib/QtXml.framework/Versions/4.0/QtXml $BUNDLE_FRAMEWORK_PATH/QtXml.framework/Versions/4.0/QtXml
cp $QTDIR/lib/QtSvg.framework/Versions/4.0/QtSvg $BUNDLE_FRAMEWORK_PATH/QtSvg.framework/Versions/4.0/QtSvg

cp $QTDIR/lib/$LIBQTDESIGNER $BUNDLE_FRAMEWORK_PATH/$LIBQTDESIGNER
cp /sw/lib/libgnutls.12.dylib /sw/lib/libgcrypt.11.dylib /sw/lib/libtasn1.2.dylib /sw/lib/libpth.14.dylib /sw/lib/libgpg-error.0.dylib $BUNDLE_FRAMEWORK_PATH
cp /sw/lib/libgmodule-2.0.0.dylib /sw/lib/libintl.1.dylib /sw/lib/libintl.3.dylib /sw/lib/libiconv.2.dylib /sw/lib/libglib-2.0.0.dylib /sw/lib/libgthread-2.0.0.dylib $BUNDLE_FRAMEWORK_PATH

cp $BUNDLE_BUILD_PATH/phspeexplugin.dylib $BUNDLE_BUILD_PATH/libspeex.1.dylib $BUNDLE_FRAMEWORK_PATH/phapi-plugins

##
# Change install name of qtBUNDLEphone
##
changeBUNDLEPhoneInstallName "/sw/lib/libglib-2.0.0.dylib"
changeBUNDLEPhoneInstallName "/sw/lib/$INTLLIB"
changeBUNDLEPhoneInstallName "/sw/lib/libintl.1.dylib"
changeBUNDLEPhoneInstallName "/sw/lib/libiconv.2.dylib"
changeBUNDLEPhoneInstallName "/sw/lib/libgthread-2.0.0.dylib"
changeBUNDLEPhoneInstallName "/sw/lib/libgmodule-2.0.0.dylib"
changeBUNDLEPhoneInstallName "/sw/lib/libgnutls.12.dylib"
changeBUNDLEPhoneInstallName "$QTDIR/$LIBQTDESIGNER"
changeBUNDLEPhoneInstallName "$QTDIR/lib/QtCore.framework/Versions/4.0/QtCore" "QtCore.framework/Versions/4.0/QtCore"
changeBUNDLEPhoneInstallName "$QTDIR/lib/QtGui.framework/Versions/4.0/QtGui" "QtGui.framework/Versions/4.0/QtGui"
changeBUNDLEPhoneInstallName "$QTDIR/lib/QtXml.framework/Versions/4.0/QtXml" "QtXml.framework/Versions/4.0/QtXml"
changeBUNDLEPhoneInstallName "$QTDIR/lib/QtSvg.framework/Versions/4.0/QtSvg" "QtSvg.framework/Versions/4.0/QtSvg"

##
# Change install name of copied libraries
##
install_name_tool -change "$QTDIR/lib/QtCore.framework/Versions/4.0/QtCore" "$BUNDLE_FRAMEWORK_PREFIX/QtCore.framework/Versions/4.0/QtCore" "$BUNDLE_FRAMEWORK_PATH/QtGui.framework/Versions/4.0/QtGui"
install_name_tool -change "$QTDIR/lib/QtCore.framework/Versions/4.0/QtCore" "$BUNDLE_FRAMEWORK_PREFIX/QtCore.framework/Versions/4.0/QtCore" "$BUNDLE_FRAMEWORK_PATH/QtXml.framework/Versions/4.0/QtXml"
install_name_tool -change "$QTDIR/lib/QtCore.framework/Versions/4.0/QtCore" "$BUNDLE_FRAMEWORK_PREFIX/QtCore.framework/Versions/4.0/QtCore" "$BUNDLE_FRAMEWORK_PATH/QtSvg.framework/Versions/4.0/QtSvg"
install_name_tool -change "$QTDIR/lib/QtXml.framework/Versions/4.0/QtXml" "$BUNDLE_FRAMEWORK_PREFIX/QtXml.framework/Versions/4.0/QtXml" "$BUNDLE_FRAMEWORK_PATH/QtSvg.framework/Versions/4.0/QtSvg"
install_name_tool -change "$QTDIR/lib/QtGui.framework/Versions/4.0/QtGui" "$BUNDLE_FRAMEWORK_PREFIX/QtGui.framework/Versions/4.0/QtGui" "$BUNDLE_FRAMEWORK_PATH/QtSvg.framework/Versions/4.0/QtSvg"
install_name_tool -change "$QTDIR/lib/QtCore.framework/Versions/4.0/QtCore" "$BUNDLE_FRAMEWORK_PREFIX/QtCore.framework/Versions/4.0/QtCore" "$BUNDLE_FRAMEWORK_PATH/$LIBQTDESIGNER"
install_name_tool -change "$QTDIR/lib/QtXml.framework/Versions/4.0/QtXml" "$BUNDLE_FRAMEWORK_PREFIX/QtXml.framework/Versions/4.0/QtXml" "$BUNDLE_FRAMEWORK_PATH/$LIBQTDESIGNER"
install_name_tool -change "$QTDIR/lib/QtGui.framework/Versions/4.0/QtGui" "$BUNDLE_FRAMEWORK_PREFIX/QtGui.framework/Versions/4.0/QtGui" "$BUNDLE_FRAMEWORK_PATH/$LIBQTDESIGNER"

changeInstallName "/sw/lib/libglib-2.0.0.dylib" "$BUNDLE_FRAMEWORK_PATH/libgmodule-2.0.0.dylib"
changeInstallName "/sw/lib/libiconv.2.dylib" "$BUNDLE_FRAMEWORK_PATH/libgmodule-2.0.0.dylib"
changeInstallName "/sw/lib/libintl.3.dylib" "$BUNDLE_FRAMEWORK_PATH/libgmodule-2.0.0.dylib"

changeInstallName "/sw/lib/libglib-2.0.0.dylib" "$BUNDLE_FRAMEWORK_PATH/libgthread-2.0.0.dylib"
changeInstallName "/sw/lib/libiconv.2.dylib" "$BUNDLE_FRAMEWORK_PATH/libgthread-2.0.0.dylib"
changeInstallName "/sw/lib/$INTLLIB" "$BUNDLE_FRAMEWORK_PATH/libgthread-2.0.0.dylib"

changeInstallName "/sw/lib/libiconv.2.dylib" "$BUNDLE_FRAMEWORK_PATH/libglib-2.0.0.dylib"
changeInstallName "/sw/lib/$INTLLIB" "$BUNDLE_FRAMEWORK_PATH/libglib-2.0.0.dylib"

changeInstallName "/sw/lib/libiconv.2.dylib" "$BUNDLE_FRAMEWORK_PATH/$INTLLIB"

changeInstallName "/sw/lib/libiconv.2.dylib" "$BUNDLE_FRAMEWORK_PATH/libintl.1.dylib"

changeInstallName "/sw/lib/libintl.1.dylib" "$BUNDLE_FRAMEWORK_PATH/libgpg-error.0.dylib"
changeInstallName "/sw/lib/libiconv.2.dylib" "$BUNDLE_FRAMEWORK_PATH/libgpg-error.0.dylib"

changeInstallName "/sw/lib/libtasn1.2.dylib" "$BUNDLE_FRAMEWORK_PATH/libgnutls.12.dylib"
changeInstallName "/sw/lib/libgcrypt.11.dylib" "$BUNDLE_FRAMEWORK_PATH/libgnutls.12.dylib"
changeInstallName "/sw/lib/libpth.14.dylib" "$BUNDLE_FRAMEWORK_PATH/libgnutls.12.dylib"
changeInstallName "/sw/lib/$INTLLIB" "$BUNDLE_FRAMEWORK_PATH/libgnutls.12.dylib"
changeInstallName "/sw/lib/libiconv.2.dylib" "$BUNDLE_FRAMEWORK_PATH/libgnutls.12.dylib"
changeInstallName "/sw/lib/libgpg-error.0.dylib" "$BUNDLE_FRAMEWORK_PATH/libgnutls.12.dylib"

changeInstallName "/sw/lib/libpth.14.dylib" "$BUNDLE_FRAMEWORK_PATH/libgcrypt.11.dylib"
changeInstallName "/sw/lib/libgpg-error.0.dylib" "$BUNDLE_FRAMEWORK_PATH/libgcrypt.11.dylib"
changeInstallName "/sw/lib/libintl.1.dylib" "$BUNDLE_FRAMEWORK_PATH/libgcrypt.11.dylib"
changeInstallName "/sw/lib/libiconv.2.dylib" "$BUNDLE_FRAMEWORK_PATH/libgcrypt.11.dylib"

install_name_tool -change "/usr/local/lib/libspeex.1.dylib" "libspeex.1.dylib" "$BUNDLE_FRAMEWORK_PATH/phapi-plugins/phspeexplugin.dylib"

##
# Copy resources files
##
cp -r $BUNDLE_BUILD_PATH/sounds $BUNDLE_RESOURCES_PATH/
cp -r $BUNDLE_BUILD_PATH/emoticons $BUNDLE_RESOURCES_PATH/
cp -r $BUNDLE_BUILD_PATH/pics $BUNDLE_RESOURCES_PATH/

##
# Copy QT Plugins
##
cp -r $QTDIR/plugins/imageformats/libqmng.dylib $BUNDLE_FRAMEWORK_PATH/qt-plugins/imageformats/libqmng.dylib
install_name_tool -change "$QTDIR/lib/QtCore.framework/Versions/4.0/QtCore" "$BUNDLE_FRAMEWORK_PREFIX/QtCore.framework/Versions/4.0/QtCore" "$BUNDLE_FRAMEWORK_PATH/qt-plugins/imageformats/libqmng.dylib"
install_name_tool -change "$QTDIR/lib/QtGui.framework/Versions/4.0/QtGui" "$BUNDLE_FRAMEWORK_PREFIX/QtGui.framework/Versions/4.0/QtGui" "$BUNDLE_FRAMEWORK_PATH/qt-plugins/imageformats/libqmng.dylib"



